package com.example.guava;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.MoreExecutors;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CacheLoaderTest {

    Employee findEmployeeByName(final String name) {
        System.out.println("findEmployeeByNameFromDB");
        return new Employee(name, name, name);
    }

    @Test
    public void basic() throws ExecutionException {
        LoadingCache<String, Employee> cache = CacheBuilder.newBuilder().maximumSize(30)
                .build(new CacheLoader<String, Employee>() {
                    @Override
                    public Employee load(String key) throws Exception {
                        return findEmployeeByName(key);
                    }
                });
        //ListeningScheduledExecutorService.
        cache.get("zhangsan");
        cache.get("zhangsan");
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        //executor.scheduleAtFixedRate()
        //ScheduledThreadPoolExecutor service = Executors.newScheduledThreadPool(1);
        ScheduledExecutorService service = MoreExecutors.getExitingScheduledExecutorService(executor, 1, TimeUnit.HOURS);
        //service.scheduleAtFixedRate()
    }
}
